package com.dotstone.chipism.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.util.Log;

import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.SocketManager;

public class Room {
	private String roomId;
	private int createTime;
	private String userId;
	private int roomType;
	private String roomName;
	private float Angle1;
	private float Angle2;
	private List<Device> devices;
	// private List<Device> mWlans = new ArrayList<Device>();
	private List<String> mMids = new ArrayList<String>();
	private List<Cmd> cmds = new ArrayList<Cmd>();
	private HashMap<String, List<Device>> deviceMap = new HashMap<String, List<Device>>();
	private MySocket socket;
	private boolean select;
	private int status;

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public boolean isSelect() {
		return select;
	}

	public void setSelect(boolean select) {
		this.select = select;
	}

	public String getRoomId() {
		return roomId;
	}

	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}

	public int getCreateTime() {
		return createTime;
	}

	public void setCreateTime(int createTime) {
		this.createTime = createTime;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public int getRoomType() {
		return roomType;
	}

	public void setRoomType(int roomType) {
		this.roomType = roomType;
	}

	public String getRoomName() {
		return roomName;
	}

	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}

	public List<Device> getDevices() {
		return devices;
	}

	public void setdevices(List<Device> devices) {
		this.devices = devices;
	}

	public float getAngle1() {
		return Angle1 % 360;
	}

	public void setAngle1(float angle1) {
		this.Angle1 = angle1;
	}

	public float getAngle2() {
		return Angle2 % 360;
	}

	public void setAngle2(float angle2) {
		this.Angle2 = angle2;
	}

	public List<Cmd> getCmds() {
		return cmds;
	}

	public List<String> getmMids() {
		return mMids;
	}

	public void setmMids(List<String> mMids) {
		this.mMids = mMids;
	}

	public void setCmds(List<Cmd> cmds) {
		this.cmds = cmds;
	}

	public Room(String roomId, String roomName, List<Device> devices) {
		super();
		this.roomId = roomId;
		this.roomName = roomName;
		this.devices = devices;
//		getMainId();
//		getCmd();
//		getCmd1();
	}

	// 为组别中的各个设备找到mainDeviceId
	public void getMainId() {
		List<Device> mDevices = DeviceManager.getInstance().getRFDevice();
//		Log.i("wmy", "mDevices" + mDevices.size() + " " + devices.size());
		for (int i = 0; i < devices.size(); i++) {
			for (Device device : mDevices) {
				if (device.getDeviceID().equals(devices.get(i).getDeviceID())) {
					devices.get(i).setMainDeviceId(device.getMainDeviceId());
					boolean e = false;
					for (int j = 0; j < mMids.size(); j++) {
						if (device.getMainDeviceId().equals(mMids.get(j))) {
							e = true;
						}
					}
					if (!e) {
						mMids.add(device.getMainDeviceId());
					}
//					Log.i("wmy", "mMids.size() = "+mMids.size());
				}
			}
		}
	}

	public void open() {
		// 发送开的指令
		// 遍历该组别的指令
		int i = 0;
		while (i < cmds.size()) {
			MySocket socket = SocketManager.getInstance().sMap.get(cmds.get(i).getMainDeviceId());
			if (cmds.get(i).getOpenCmdss().length() != 0) {
				socket.sendMqttData(cmds.get(i).getOpenCmdss());
			}
			if (cmds.get(i).getOpenCmdl().length() != 0) {
				socket.sendMqttData(cmds.get(i).getOpenCmdl());
			}
			try {
				Thread.sleep(150);
				i++;
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void close() {
		int i = 0;
		while (i < cmds.size()) {
			try {
				MySocket socket = SocketManager.getInstance().sMap.get(cmds.get(i).getMainDeviceId());
				if (socket != null) {
					if (cmds.get(i).getCloseCmdss().length() != 0) {
						socket.sendMqttData(cmds.get(i).getCloseCmdss());
					}
					if (cmds.get(i).getCloseCmdl().length() != 0) {
						socket.sendMqttData(cmds.get(i).getCloseCmdl());
					}
				}
				i++;
				Thread.sleep(150);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void getCmd1() {
		// List<Device> mWlans = DeviceManager.getInstance().getWlans();
//		Log.e("wmy", "getCmd1 "+mMids.size());
		for (int i = 0; i < mMids.size(); i++) {
			Cmd cmd = new Cmd();
			cmd.setMainDeviceId(mMids.get(i));
			List<Device> ssDevices = new ArrayList<Device>();
			List<Device> lDevices = new ArrayList<Device>();
			for (int n = 0; n < devices.size(); n++) {
				if (devices.get(n).getMainDeviceId().equals(mMids.get(i))) {
					switch (devices.get(n).getDeviceType()) {
					case DeviceType.DEVICE_TYPE_SWITCH:
					case DeviceType.DEVICE_TYPE_SOCKET:
						ssDevices.add(devices.get(n));
						break;
					case DeviceType.DEVICE_TYPE_RGB_LIGHT:
						lDevices.add(devices.get(n));
						break;
					default:
						break;
					}
				}
			}
			cmd.setmSSDevices(ssDevices);
			cmd.setmLightDevices(lDevices);
			cmds.add(cmd);
		}
	}

	public void getCmd() {
		// 遍历devices设备集合 获取每一个设备的主id
		// 将其放到以主id为key的设备集合中
		for (int i = 0; i < devices.size(); i++) {
			if (deviceMap.get(devices.get(i).getMainDeviceId()) == null) {
				deviceMap.put(devices.get(i).getMainDeviceId(), new ArrayList<Device>());
				deviceMap.get(devices.get(i).getMainDeviceId()).add(devices.get(i));
			} else {
				deviceMap.get(devices.get(i).getMainDeviceId()).add(devices.get(i));
			}
		}

	}

}
